//
//  IndicatorItem.swift
//  MySwiftPullToRefresh
//
//  Created by likai on 2017/5/3.
//  Copyright © 2017年 terry. All rights reserved.
//

import UIKit

//final 关键字--类或方法不希望被继承和重写
final class IndicatorItem{
    private let color:UIColor
    //http://www.cocoachina.com/ios/20161202/18252.html
    //动画黄金搭档:CADisplayLink & CAShapeLayer
    //有CAShapeLayer概念的介绍
    lazy var arrowLayer: CAShapeLayer = {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 8))
        path.addLine(to: CGPoint(x: 0, y: -8))
        path.move(to: CGPoint(x: 0, y: 8))
        path.addLine(to: CGPoint(x: 5.66, y: 2.34))
        path.move(to: CGPoint(x: 0, y: 8))
        path.addLine(to: CGPoint(x: -5.66, y: 2.34))
        
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.strokeColor = self.color.cgColor
        layer.lineWidth = 2
        layer.lineCap = kCALineCapRound
        
        return layer
    }()
    
    let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    
    init(color:UIColor) {
        self.color = color
        
        indicator.color = color
    }
    
    func updateRefreshState(_ isRefreshing: Bool){
        arrowLayer.isHidden = isRefreshing
        isRefreshing ? indicator.startAnimating() : indicator.stopAnimating()
        
        
        
    }
    func updateProgress(_ progress: CGFloat, isHeader: Bool = true){
        if isHeader {
            arrowLayer.transform = progress == 1 ? CATransform3DMakeRotation(CGFloat.pi, 0, 0, 1) : CATransform3DIdentity
            
        }else {
            
            arrowLayer.transform = progress == 1 ? CATransform3DIdentity : CATransform3DMakeRotation(CGFloat.pi, 0, 0, 1)
        }
        
    }
    
    
}
